

%macro w2_iris(year);

  libname w2_&year "~/adrec/irs_iw2/&year";

  DATA work.w2_dups_flagged;
    SET w2_&year..iw2ty&year._dups_flagged;
  RUN;

  PROC SORT DATA=work.w2_dups_flagged; BY pik; RUN;
  PROC SORT DATA=work.piks; BY pik; RUN;
  DATA work.pik_match_w2_dups_flagged (KEEP = ein);
    MERGE work.w2_dups_flagged (IN=A) work.piks (IN=B);
    BY pik;
    IF A=1 & B=1 THEN _merge=3;
    IF A=1 & B=0 THEN _merge=1;
    IF A=0 & B=1 THEN _merge=2;
    IF _merge=1 THEN delete;
    IF _merge=2 THEN delete;
    IF _merge=3 THEN flag_inw2=1;
    DROP _merge;
  RUN;

  DATA work.w2_summaryrecs;
    SET w2_&year..iw2ty&year._summaryrecs;
  RUN;

  PROC SORT DATA=work.w2_summaryrecs; BY pik; RUN;
  PROC SORT DATA=work.piks; BY pik; RUN;
  DATA work.pik_match_w2_summaryrecs (KEEP = ein);
    MERGE work.w2_summaryrecs (IN=A) work.piks (IN=B);
    BY pik;
    IF A=1 & B=1 THEN _merge=3;
    IF A=1 & B=0 THEN _merge=1;
    IF A=0 & B=1 THEN _merge=2;
    IF _merge=1 THEN delete;
    IF _merge=2 THEN delete;
    IF _merge=3 THEN flag_inw2=1;
    DROP _merge;
  RUN;

  DATA work.w2_ein_list;
    SET work.pik_match_w2_dups_flagged
       work.pik_match_w2_summaryrecs;
  RUN;

  PROC SQL;
    CREATE TABLE work.w2_ein_list_&year AS
    SELECT DISTINCT ein
    FROM work.w2_ein_list
    WHERE ein IS NOT MISSING;
  QUIT;

  PROC SORT DATA=work.w2_ein_list_&year; BY ein; RUN;

%mend;







%macro w2_ssl_ein_iris(year);

  libname ssl "~/economic/ssl/&year";

  DATA work.ssl_mu;
    SET ssl.ssl&year.mu;
  RUN;

  DATA work.ssl_su;
    SET ssl.ssl&year.su;
  RUN;

  PROC SORT DATA=work.ssl_mu; BY ein; RUN;
  PROC SORT DATA=work.ssl_su; BY ein; RUN;

  DATA work.match_ssl_mu;
    MERGE work.ssl_mu (IN=A) work.w2_ein_list_full (IN=B);
    BY ein;
    IF A=1 & B=1 THEN _merge=3;
    IF A=1 & B=0 THEN _merge=1;
    IF A=0 & B=1 THEN _merge=2;
    IF _merge=1 THEN DELETE;
    IF _merge=2 THEN DELETE;
    DROP _merge;
  RUN;

  DATA work.match_ssl_su;
    MERGE work.ssl_su (IN=A) work.w2_ein_list_full (IN=B);
    BY ein;
    IF A=1 & B=1 THEN _merge=3;
    IF A=1 & B=0 THEN _merge=1;
    IF A=0 & B=1 THEN _merge=2;
    IF _merge=1 THEN DELETE;
    IF _merge=2 THEN DELETE;
    DROP _merge;
  RUN;


  PROC EXPORT
    DATA = work.match_ssl_mu
    OUTFILE = "projectdir/data/raw_pulls/w2_ssl_usiris/w2_ssl_mu_&year._usiris_2018q4.dta"
    DBMS = DTA
    REPLACE;
  RUN;

  PROC EXPORT
    DATA = work.match_ssl_su
    OUTFILE = "projectdir/data/raw_pulls/w2_ssl_usiris/w2_ssl_su_&year._usiris_2018q4.dta"
    DBMS = DTA
    REPLACE;
  RUN;

%mend;



libname iris "~/adrec/university_employee_usiris/2018";
proc sql;
  create table work.piks as
  select distinct pik
  from iris.usiris_emnmxwalk_2018q4a_2018q4a
  where pik IS NOT MISSING;
quit;

%w2_iris(2005);
%w2_iris(2006);
%w2_iris(2007);
%w2_iris(2008);
%w2_iris(2009);
%w2_iris(2010);
%w2_iris(2011);
%w2_iris(2012);
%w2_iris(2013);
%w2_iris(2014);
%w2_iris(2015);
%w2_iris(2016);
%w2_iris(2017);


DATA work.w2_ein_list_full;
  SET work.w2_ein_list_2005
      work.w2_ein_list_2006
      work.w2_ein_list_2007
      work.w2_ein_list_2008
      work.w2_ein_list_2009
      work.w2_ein_list_2010
      work.w2_ein_list_2011
      work.w2_ein_list_2012
      work.w2_ein_list_2013
      work.w2_ein_list_2014
      work.w2_ein_list_2015
      work.w2_ein_list_2016
      work.w2_ein_list_2017;
RUN;

PROC SQL;
  CREATE TABLE work.w2_ein_list_full AS
  SELECT DISTINCT ein
  FROM work.w2_ein_list_full
  WHERE ein IS NOT MISSING;
QUIT;



%w2_ssl_ein_iris(2000);
%w2_ssl_ein_iris(2001);
%w2_ssl_ein_iris(2002);
%w2_ssl_ein_iris(2003);
%w2_ssl_ein_iris(2004);
%w2_ssl_ein_iris(2005);
%w2_ssl_ein_iris(2006);
%w2_ssl_ein_iris(2007);
%w2_ssl_ein_iris(2008);
%w2_ssl_ein_iris(2009);
%w2_ssl_ein_iris(2010);
%w2_ssl_ein_iris(2011);
%w2_ssl_ein_iris(2012);
%w2_ssl_ein_iris(2013);
%w2_ssl_ein_iris(2014);
%w2_ssl_ein_iris(2015);
%w2_ssl_ein_iris(2016);



